package com.cl.park.service.impl;

import java.util.List;

import com.cl.park.dao.BaseDao;
import com.cl.park.model.Admin;
import com.cl.park.model.Entrance;
import com.cl.park.model.Floor;
import com.cl.park.model.Park;
import com.cl.park.model.Rule;
import com.cl.park.service.AdminService;

public class AdminServiceImpl implements AdminService{
	
	public BaseDao baseDao;
	
	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}

	public Admin adminLogin(String userName,String passWord){
		Admin admin = (Admin)this.baseDao.getByNProperty(Admin.class, "userName",userName,"passWord",passWord);
		if(admin==null){
			return null;
		}
		return admin;
	}
	
	public String addVP(Admin admin) {
		
		if(this.baseDao.doHqlForOne("FROM Admin WHERE userName = '"+admin.getUserName()+"'")==null){
			admin.setType(2);
			this.baseDao.add(admin);
			return "添加成功";
		}else{
			return "用户名已存在";
		}
	}
	
	public List<Object> adminList(){
		List<Object> list = this.baseDao.getListByHql("FROM Admin WHERE type = 2");
		return list;
	}

	public Admin getAdmin(Integer id) {
		return (Admin)this.baseDao.get(Admin.class, id);
	}

	public String updateAdmin(Admin admin) {
		Admin later = (Admin)this.baseDao.get(Admin.class, admin.getId());
		later.setPassWord(admin.getPassWord());
		later.setRealName(admin.getRealName());
		this.baseDao.update(later);
		return "ok";
	}

	public String addEntrance(Entrance ent) {
		Entrance ents = (Entrance)this.baseDao.getByNProperty(Entrance.class, "name",ent.getName());
		if(ents!=null){
			return "notOnly";
		}
		this.baseDao.add(ent);
		return "ok";
	}

	public String addFloor(Floor floor) {
		Floor floors = (Floor)this.baseDao.getByNProperty(Floor.class, "floor",floor.getFloor().toString());
		if(floors!=null){
			return "notOnly";
		}
		this.baseDao.add(floor);
		return "ok";
	}

	public String addPrak(Park park) {
		Park parks = (Park)this.baseDao.getByNProperty(Park.class, "floor",park.getName());
		if(parks!=null){
			return "notOnly";
		}
		park.setCar(0);
		park.setState(1);
		String[] abc = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
		park.setName(abc[park.getFloor()]+park.getId());
		this.baseDao.add(park);
		Floor floor = (Floor)this.baseDao.get(Floor.class, park.getFloor());
		floor.setCount(floor.getCount()+1);
		floor.setFree(floor.getFree()+1);
		this.baseDao.update(floor);
		
		return "ok";
	}

	public List<Object> entranceList() {
		List<Object> list = this.baseDao.getListByHql("FROM Entrance");
		return list;
	}

	public List<Object> floorList() {
		List<Object> list = this.baseDao.getListByHql("FROM Floor");
		return list;
	}

	public List<Object> parkList(Park park) {
		String hql = "FROM Park WHERE 1=1";
		if(null != park){
			if(park.getFloor()!=null){
				hql+=" AND floor = "+park.getFloor();
			}
			if(park.getName()!=null && !park.getName().equals("")){
				hql+=" AND name = '"+park.getName()+"'";
			}
			if(park.getState()!=null){
				hql+=" AND state = "+park.getState();
			}
		}
		List<Object> list = this.baseDao.getListByHql(hql);
		return list;
	}
	
	public Park getPark(Park park) {
		String hql = "FROM Park WHERE 1=1";
		if(null != park){
			if(park.getFloor()!=null){
				hql+=" AND floor = "+park.getFloor();
			}
			if(park.getName()!=null && !park.getName().equals("")){
				hql+=" AND name = '"+park.getName()+"'";
			}
			if(park.getState()!=null){
				hql+=" AND state = "+park.getState();
			}
		}
		List<Object> list = this.baseDao.getListByHql(hql);
		return null == list ? null : (Park)list.get(0);
	}

	public Entrance getEntranceById(Integer id) {
		return (Entrance) baseDao.get(Entrance.class, id);
	}

	public Floor getFloorById(Integer id) {
		return (Floor) baseDao.get(Floor.class, id);
	}

	public Park getParkById(Integer id) {
		return (Park) baseDao.get(Park.class, id);
	}

	public boolean updateEntrance(Entrance entrance) {
		Entrance temp = getEntranceById(entrance.getId());
		temp.setName(entrance.getName());
		temp.setState(entrance.getState());
		baseDao.update(temp);
		return true;
	}

	public boolean updateFloor(Floor floor) {
		Floor temp = getFloorById(floor.getId());
		temp.setCount(floor.getCount());
		temp.setFloor(floor.getFloor());
		temp.setFree(floor.getFree());
		temp.setUsed(floor.getUsed());
		baseDao.update(temp);
		return false;
	}

	public boolean updatePark(Park park) {
		Park temp = getParkById(park.getId());
		temp.setCar(park.getCar());
		temp.setFloor(park.getFloor());
		temp.setName(park.getName());
		temp.setState(park.getState());
		temp.setType(park.getType());
		baseDao.update(temp);
		return false;
	}

	public Rule getRuleById(Integer id) {
		return (Rule) baseDao.get(Rule.class, id);
	}

	public boolean updateRule(Rule rule) {
		Rule temp = getRuleById(rule.getId());
		temp.setDescribe(rule.getDescribe());
		temp.setMoney(rule.getMoney());
		temp.setName(rule.getName());
		temp.setType(rule.getType());
		baseDao.update(temp);
		return true;
	}

}
